﻿namespace Vdc.Grid
{
    using System;
    using System.Text;

    internal class JSHelper
    {
        public static string AddStyleSheet(string pathToCssFile, string gridId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("var " + gridId + "_JSH_head=document.getElementsByTagName('head')[0];");
            builder.Append("var " + gridId + "_JSH_styleSheets=" + gridId + "_JSH_head.getElementsByTagName('link');");
            builder.Append("var " + gridId + "_JSH_styleSheetsFound=false;");
            builder.Append("for(var i=0; i<" + gridId + "_JSH_styleSheets.length; i++){if(" + gridId + "_JSH_styleSheets[i].href.indexOf(\"" + pathToCssFile + "\") != -1){" + gridId + "_JSH_styleSheetsFound=true;break;}};");
            builder.Append("if(!" + gridId + "_JSH_styleSheetsFound){");
            builder.Append("var tempStyleSheet=document.createElement('link');");
            builder.Append("tempStyleSheet.rel='stylesheet';");
            builder.Append("tempStyleSheet.type='text/css';");
            builder.Append("tempStyleSheet.href=\"" + pathToCssFile + "\";");
            builder.Append("document.getElementsByTagName('head')[0].appendChild(tempStyleSheet);");
            builder.Append("}");
            return builder.ToString();
        }
    }
}

